var app = getApp();
Page(Object.assign({}, {
    data:{
        inited : false,
        form : {},
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        paynumber : '',
        paytype : 0,
        payyear : '',
        paymonth : '',
        paycvc : '',
    },
    onLoad:function(options){
        var self = this;
		
		var shopid = options.shopid ? options.shopid : 0;
        this.data.options = options;

        app.com.auth(function(user){
            app.com.http('user_v1','POST',{op:'addmstart'},0,false,'','',function(res){
                if (res.data && !res.data.errno) {
                    self.setData({
                        Pagetheme : res.data.data.params.style,
                        sharedata : res.data.data.sharedata,
                        params : res.data.data.params,
                        paytype : res.data.data.params.paytype,
                        paycard : res.data.data.paycard ? res.data.data.paycard : [],
                        inited : true,
                    });
                    wx.setNavigationBarTitle({
                        title:  '充值',
                    });
                }else{
                    app.com.alert(res.data.message);
                }
            });
			app.com.setBar(app,self,shopid);
        },self);
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: '',
        }
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.data.isdown = true;
        this.onLoad( this.data.options );
        this.data.isdown = false;
        this.data.payed = false;
        wx.stopPullDownRefresh();
    },
    addthis : function(e){
        this.setData({
            money : e.currentTarget.dataset.m
        })
    },
    confirm(e){
        var self = this;
        
        if( self.data.money*1 <= 0 || !self.data.money ) {
            app.com.toast('请填写充值金额');
            return false;
        }
        
        var postdata = {
            op:'payadd',
            money : self.data.money,
            paytype : self.data.paytype,
            paynumber : self.data.paynumber,
            paycid : self.data.paycid,
        };

        if( self.data.payed ) return false;
        self.data.payed = true;

        app.com.http('user_v1','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno && res.data) {
                
                if(res.data.data.payed == 1){
                    app.com.toast('支付完成','none',function(){
                        wx.navigateBack();
                    });
                    return false;
                }

                wx.requestPayment({
					'appId' : res.data.data.appId,
                    'timeStamp': res.data.data.timeStamp,
                    'nonceStr': res.data.data.nonceStr,
                    'package': res.data.data.package,
                    'signType': 'MD5',
                    'paySign': res.data.data.paySign,
                    'success': function (payres) {
                        if( payres.errMsg == 'requestPayment:ok' ){
                            app.com.toast('支付完成','none',function(){
                                wx.navigateBack();
                            });
                        }
                    },
                    'fail': function (payres) {
                        
                    }
                });
                self.data.payed = false;

            }else if( res.data.errno == 2 ){
                app.com.confirm(res.data.message,function(){
                    var tourl = '/zofui_sales/ucard/buycard';
                    wx.navigateTo({
                      url: tourl
                    });
                },false,'取消','购买会员');

            }else{
                app.com.alert(res.data.message);
            }
            self.data.payed = false;
        });
        
    },
    money : function(e){
        this.setData({
            money : e.detail.value
        })
    },
    showcode(){
        this.selectComponent("#bindparent").showfn();
    },
    subbindparent(e){
        var self = this;
        if(!e.detail){
            app.com.toast('请输入卡密');
            self.selectComponent("#bindparent").showfn();
            return false;
        }
        var postdata = {
            code: e.detail,
            op : 'passbycode'
        };
        app.com.http('user_v1','POST',postdata,0,true,'','',function(res){
            if (res.data && !res.data.errno) {
                app.com.toast('已成功兑换');
                
            }else{
                app.com.alert(res.data.message);
                self.selectComponent("#bindparent").showfn();
            }
        });
    },
    showpaycard(){
        this.selectComponent('#paycard').show();
    },
    setpaycard(e){
        var self = this;
        self.setData({
            paynumber : e.detail.paynumber,
            paycid : e.detail.paycid,
        })
    },
    checkpay(e){
        this.setData({
            paytype : e.currentTarget.dataset.type*1,
        })
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
}))